<?php
namespace Qy {
    /**
    * 企业微信会话服务类
    * @author Jamers
    * @since 2017.1.1
    * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%9A%E8%AF%9D%E6%9C%8D%E5%8A%A1
    */
    class Chat {
        private $common;
        
        public function __construct($ary = array()) {
            if (isset($ary['common'])) {
                $this->common = &$ary['common'];
            }else{
                $this->common = new \Qy\Common();
            }
        }
        /**
        * 创建会话
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E4%BC%9A%E8%AF%9D%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function createChat($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['chatid']) || !isset($ary['name']) || !isset($ary['owner']) || !isset($ary['userlist'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/chat/create?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        /**
        * 获取会话
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E4%BC%9A%E8%AF%9D%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function getChat($chatid, $a_token = '') {
            if (empty($chatid)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/chat/get?access_token={$a_token}&chatid={$chatid}";
            return $this->common->httpRequest($url);
        }
        
        /**
        * 修改会话信息
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E4%BC%9A%E8%AF%9D%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function updateChat($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['chatid']) || !isset($ary['op_user'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/chat/update?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }

        /**
        * 退出会话
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E4%BC%9A%E8%AF%9D%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function quitChat($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['chatid']) || !isset($ary['op_user'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/chat/quit?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }

        /**
        * 清除会话未读状态
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E4%BC%9A%E8%AF%9D%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function clearNotifyChat($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['chat']) || !isset($ary['op_user'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/chat/clearnotify?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }

        /**
        * 发送消息
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E4%BC%9A%E8%AF%9D%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function sendChat($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['receiver']) || !isset($ary['sender'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/chat/send?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }    

        /**
        * 设置成员新消息免打扰
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BC%81%E4%B8%9A%E4%BC%9A%E8%AF%9D%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function setmuteChat($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['user_mute_list'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/chat/setmute?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
    }
}
?>
